package com.link.base.base.sms.controller;

import com.link.base.base.sms.model.AccntGrouping;
import com.link.base.base.sms.service.AccntGroupingService;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.Filter;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: liulu
 * @Date:15:37 2018/11/5
 */
@Controller
@RequestMapping("/link/accntGrouping")
public class AccntGroupingController extends BasicController<AccntGrouping> {

    @Resource
    private AccntGroupingService accntGroupingService;

    @Override
    public BasicService<AccntGrouping> getBasicService() throws Exception {
        return accntGroupingService;
    }

    /**
     * 新建分组
     * 按条件
     */
    @RequestMapping(value = "/inserFiltrateCondition", method = RequestMethod.POST, produces = "application/json"
        )
    @ResponseBody
    public void inserFiltrateCondition(@RequestBody AccntGrouping entity, HttpSession session,
                                       HttpServletRequest request, HttpServletResponse response) {

        Map<String, Object> result = new HashMap<String, Object>(8);
        try {
            QueryParams qps = new QueryParams();
            qps.setPageFlag(false);
            qps.setTotalFlag(false);
            if (!StringUtils.isNull(entity.getGpName())) {
                qps.addFilter(new Filter("gpName", Filter.OP_EQUAL, entity.getGpName()));
                AccntGrouping accntGrouping = (AccntGrouping) BasicModel.transformClass(entity, qps);
                List<AccntGrouping> aca = accntGroupingService.queryByExamplePage(accntGrouping);

                if (aca.size() > 0) {
                    result.put("result", "组名已存在");
                } else {

                    accntGroupingService.insert(entity);
                }
                result.put("success", true);
            }
        } catch (ServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
    }


}
